package cn.sharp.bbs.service.impl;

import cn.sharp.bbs.common.Utils;
import cn.sharp.bbs.dao.admin.UserManageDao;
import cn.sharp.bbs.dao.gen.UserMapper;
import cn.sharp.bbs.entity.BO.UserQuery;
import cn.sharp.bbs.entity.DO.Role;
import cn.sharp.bbs.entity.DO.User;
import cn.sharp.bbs.entity.DO.Userrole;
import cn.sharp.bbs.service.UserManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class UserManageServiceImpl implements UserManageService {
    private final Logger log = LoggerFactory.getLogger(UserManageServiceImpl.class);
    @Resource
    UserManageDao userManageDao;
    @Resource
    UserMapper userMapper;

    /**
     * 检查用户是否无效
     * @param uid 用户id
     * @return 布尔值, 代表用户是否无效
     */
    @Override
    public boolean isInvalidUser(Long uid){
        return userMapper.selectByPrimaryKey(uid) == null;
    }

    /**
     * 获取用户详细信息
     * @param uid 用户id
     * @return 用户实体, 密码项进行了隐藏
     */
    @Override
    public User getUserDetails(Long uid) {
        User user = userMapper.selectByPrimaryKey(uid);
        user.setPwd("");
        return user;
    }

    /**
     * 获取用户列表
     * @param query 查询条件
     * @return 根据查询条件生成的用户列表页
     */
    @Override
    public Map<String, Object> getUserList(UserQuery query) {
        return Utils.getPage(query, ()-> userManageDao.queryUserList(query));
    }

    /**
     * 修改用户状态
     * @param uid 用户id
     * @param state 要修改的目标状态
     */
    @Override
    public void editUserState(Long uid, int state) {
        userManageDao.editUserState(uid, state);
    }

    /**
     * 获取用户拥有的角色列表
     * @param uid 用户id
     * @return 角色列表
     */
    @Override
    public List<Role> listUserRole(Long uid) {
        return userManageDao.listUserRole(uid);
    }



    /**
     * 修改用户的角色
     * @param uid 用户id
     * @param roles 要设置的角色id列表
     */
    @Override
    public void modifyUserRole(Long uid, Long[] roles) {
        List<Long> targetRoles = Arrays.asList(roles);
        List<Long> currentRoleIds = listUserRole(uid).stream().map(Role::getId).collect(Collectors.toList());
        List<Userrole> include = targetRoles.stream().filter(role_id->!currentRoleIds.contains(role_id))
                .map(role_id-> {
                    Userrole userRole = new Userrole();
                    userRole.setRoleId(role_id);
                    userRole.setUserId(uid);
                    return userRole;
                }).collect(Collectors.toList());
        List<Userrole> exclude = currentRoleIds.stream().filter(role_id->!targetRoles.contains(role_id))
                .map(role_id-> {
                    Userrole userRole = new Userrole();
                    userRole.setRoleId(role_id);
                    userRole.setUserId(uid);
                    return userRole;
                }).collect(Collectors.toList());
        if(include.size()>0) userManageDao.addUserRoles(include);
        if(exclude.size()>0) userManageDao.delUserRoles(exclude);
    }

    /**
     * 检查角色列表是否合法
     * @param roles 角色列表
     * @return 布尔值, 代表是否合法
     */
    @Override
    public boolean checkRoles(Long[] roles) {
        List<Role> roleList = userManageDao.listAllRole();
        log.debug(String.valueOf(roleList));
        List<Long> all_role = roleList.stream().map(Role::getId)
                .collect(Collectors.toList());
        log.debug(Arrays.toString(roles));
        log.debug(String.valueOf(all_role));
        return Arrays.stream(roles).allMatch(all_role::contains);
    }

    @Override
    public int getUserNum() {
        return userManageDao.getUserNum();
    }

    @Override
    public int getPostNum() {
        return userManageDao.getPostNum();
    }
}
